package com.badlogic.androidgames.asyncutil;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import com.badlogic.androidgames.framework.FileIO;
import com.badlogic.androidgames.framework.impl.GLGame;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;


public class AsyncBitmapLoader extends AsyncTask<URL, Integer, Bitmap> 
{
	GLGame g;
	ProgressDialog progressBar;
	
	public AsyncBitmapLoader(GLGame g)
	{
		super();
		this.g = g;
	}

	protected Bitmap doInBackground(URL... url) {
		publishProgress(1);
        Bitmap bitmap = null;
		try {
			bitmap = BitmapFactory.decodeStream(url[0].openConnection().getInputStream());
		} catch (IOException e) {	
			try {	
				FileIO fileIO;
		        fileIO = g.getFileIO();
		        InputStream in = null;
		        in = fileIO.readAsset("errorImage.png");
	            bitmap = BitmapFactory.decodeStream(in);
			} 
			catch (IOException ex) 
			{
				ex.printStackTrace();
			}
		}
		return bitmap;	
	}

	protected void onProgressUpdate(Integer... progress) 
	{
		//progressBar = new ProgressDialog(g);
		//progressBar.setMessage("Loading ...");
		//progressBar.show();
	}

	protected void onPostExecute(Bitmap res) 
	{
		//progressBar.hide();
		//progressBar.dismiss();
	}
}